<?php

namespace App\Admin\Controllers\Bureau;

use App\Admin\Actions\Grid\Bureau\RefuseClubNoticeAction;
use App\Admin\Repositories\Bureau\WxClub;
use App\Wen\Utils\Settings;
use Dcat\Admin\Actions\Action;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WxClubController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxClub(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->model()->orderBy('id','desc');
            $grid->quickSearch(['id', 'name','intro', 'user_id', 'ower.user_name', 'address_detailed'])->placeholder('搜索ID，简介，用户ID，用户名字，地址...')->width(50);
            $grid->column('owers', '头像')->display(function ($v) {
                return '<img src="' . ($this->ower['user_avatar'] ?? Settings::get('img_default', 'https://img.mini.minisns.cn/icons/dafault.png'))  . '"  style="border-radius:50px;width:30px;" />';
            })->width('50px');
            $grid->column('ower', '用户名称')->display(function ($v) {
                if($v && $v['user_name']){
                    return '<a target="_blank" href="'.admin_url('users?id='.$v['id']).'">'.$v['user_name'].'</a>' ?? '用户已删除';
                }
                return '用户已删除';
            })->width('100px');
            $grid->column('avatar')->image();
            $grid->column('name')->display(function ($value){
                if($this->status === 0){
                    if(!_empty_($this->name_tmp)){
                        return $value . " ➡️ " .$this->name_tmp;
                    }
                }
                return $value;
            });
            $grid->column('intro');
//            $grid->column('longitude');
//            $grid->column('latitude');
//            $grid->column('address_name');
            $grid->column('address_detailed');
//            $grid->column('ip');
//            $grid->column('country');
//            $grid->column('province');
//            $grid->column('city');
//            $grid->column('district');
            $grid->column('status')->switch();
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append((new RefuseClubNoticeAction())->setKey($this->id));
            });

            $grid->disableCreateButton();

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxClub(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('avatar');
            $show->field('name');
            $show->field('intro');
            $show->field('assistants');
            $show->field('longitude');
            $show->field('latitude');
            $show->field('address_name');
            $show->field('address_detailed');
            $show->field('ip');
            $show->field('country');
            $show->field('province');
            $show->field('city');
            $show->field('district');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxClub(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->image('avatar')->url('files/uploads')->uniqueName()->autoUpload()->required();
            $form->text('name');
            $form->textarea('intro');

            $form->multipleSelect('assistants')->options(function ($ids) {
                if($ids){
                    $res = [];
                    foreach ($ids as $id){
                        $user = \App\Models\User\WxUser::find($id);
                        if ($user) {
                            $res[$user->id] = $user->user_name;
                        }
                    }
                    return $res;
                }
            })->ajax('select/users')->saveAsJson();

            $form->text('longitude');
            $form->text('latitude');
            $form->text('address_name');
            $form->text('address_detailed');

            $form->text('ip');
            $form->text('country');
            $form->text('province');
            $form->text('city');
            $form->text('district');

            $form->switch('status')->default(0);

            $form->display('created_at');
            $form->display('updated_at');

            $form->saving(function (Form $form){
                if($form->isEditing()){
                    if(\App\Models\Bureau\WxClub::where('name', $form->input('name'))->where('id', '<>', $form->getKey())->exists()){
                        return $this->fail('俱乐部名称与已有的重复');
                    }
                }else if($form->isCreating()){
                    return $form->response()->error('请去前端申请');
                }
            });

            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    return $form->response()->error('权限不足，不可以删除其他分站对象');
                }
            });

        });
    }
}
